//
//  NSString+Validate.m
//  OMTCoreKit
//
//  Created by 昭荣伊 on 2018/7/27.
//

#import "NSString+Validate.h"

@implementation NSString (Validate)

- (BOOL)omt_isEmpty {
    if (!self ||
        self.length < 1 ||
        [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length < 1) return YES;
    
    return NO;
}

- (BOOL)omt_isNotEmpty {
    return ![self omt_isEmpty];
}

- (BOOL)omt_isArabic {
    if (self.length < 1) return NO;
    __block BOOL isArabic = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        if (substring.length > 0) {
            const unichar code = [substring characterAtIndex:0];
            if ([self _characterIsEmoji:code] || [self _characterIsSymbol:code]) return;
            
            if ([self _characterIsArabicChar:code]) {
                isArabic = YES;
                // 找到后终止
                *stop = YES;
                return;
            } else {
                isArabic = NO;
                // 找到后终止
                *stop = YES;
            }
        }
    }];
    return isArabic;
}

/**
 字符是符号
 
 @param unicode unicode
 @return YES 是 NO 否
 */
- (BOOL)_characterIsSymbol:(const unichar)unicode {
    BOOL isSymbol = NO;
    if (unicode >= 0x0020 && unicode <= 0x002F) {
        isSymbol = YES;
    } else if (unicode >= 0x003A && unicode <= 0x0040) {
        isSymbol = YES;
    } else if (unicode >= 0x2500 && unicode <= 0x2BFF) {
        isSymbol = YES;
    } else if (unicode >= 0x2000 && unicode <= 0x206F) {
        isSymbol = YES;
    }
    return isSymbol;
}


/**
 阿拉伯字符
 
 @param unicode unicode
 @return YES 是 NO 否
 */
- (BOOL)_characterIsArabicChar:(const unichar)unicode {
    BOOL isArabic = NO;
    if ((unicode >= 0x0600 && unicode <= 0x06FF) ||
        (unicode >= 0x0750 && unicode <= 0x077f) ||
        (unicode >= 0x08A0 && unicode <= 0x08FF) ||
        (unicode >= 0xFB50 && unicode <= 0xFDFF) ||
        (unicode >= 0xFE70 && unicode <= 0xFEFF)) {
        isArabic = YES;
    }
    return isArabic;
}


/**
 Emoji表情
 
 @param unicode unicode
 @return YES 是 NO 否
 */
- (BOOL)_characterIsEmoji:(const unichar)unicode {
    BOOL isEmoji = NO;
    if ((unicode >= 0xD800 && unicode <= 0xDB7F) ||
        (unicode >= 0x1F600 && unicode <= 0x1F64F)) {
        isEmoji = YES;
    }
    return isEmoji;
}

@end
